<?php

/**
 * 
 * @author HxBreak
 * @Description: ...
 * @Date 2017年6月7日00:36:53 
 * @File ...php
 * PDO SAFE
 * Last Modify
 * 2017年6月7日19:36:29
 */

include 'common.php';

$username = $_COOKIE['username'];
$token = $_COOKIE['token'];

$id = $_REQUEST['id'];
$action = $_REQUEST['action'];

$dbc = pdo_root_connect();

//$result = mysql_query("select * from userinfo where username='$username' and token='$token'");
$stmt = $dbc->prepare('select * from userinfo where username=:username and token=:token and status=0');
$stmt->bindParam(':username', $username);
$stmt->bindParam(':token', $token);
$stmt->execute();

$errorcode = 0;

if($stmt->rowCount() > 0)
{
	$addr = $_SERVER['REMOTE_ADDR'];
	$time = time();
	$errorcode = 105;	
	if($action == 'del')
	{
		$errorcode = 2;
#		$result2 = mysql_query("delete from userhope where username='$username' and id='$id'");
//		$result2 = mysql_query("update userwish set status='1',deltime='$time' where username='$username' and id='$id' and status='0'");
		$pre = $dbc->prepare('update userwish set status=1, deltime=unix_timestamp(now()) where username=? and id=? and status=0');
		$pre->execute(array($username, $id));
		
	}else if($action == 'share')
	{
		$errorcode = 2;
		$pre = $dbc->prepare('update userwish set publishtime=unix_timestamp(now()) where username=? and id=? and publishtime=0 and status=0');
		$pre->execute(array($username, $id));
		//$result2 = mysql_query("update userwish set publishtime='$time' where username='$username' and id='$id' and publishtime='0' and status='0'");
	}else if ($action == 'dolike'){
		$errorcode = 2;
		$dolikecount = $dbc->prepare('select * from dolikecheck where username=? and like2id=?');
//		$result_hoperow = mysql_query("select * from dolikecheck where username='$username' and like2id='$id'");
		$dolikecount->execute(array($username, $id));	
		if($dolikecount->rowCount() > 0) 
		{
			$errorcode = 103;
		}else
		{
			$likerow = $dbc->prepare('select * from userwish where id=:id and status=0');
			$likerow->bindParam(':id', $id);
			$likerow->execute();
			if($likerow->rowCount() > 0){
				$dolikecheck = $dbc->prepare('insert into dolikecheck (username, like2id, liketime) values (?, ?, unix_timestamp(now()))');
				$dolikecheck->execute(array($username, $id));
//				$likeinsert = mysql_query("insert into dolikecheck (username, like2id, liketime) values ('$username', '$id', '$time')");
				if($dolikecheck->rowCount() > 0)
				{
					$pre = $dbc->prepare('update userwish set good=good+1 where id=:id');
					//$resultuplike = mysql_query("update userwish set good=good+1 where id='$id'");
					$pre->bindParam(':id', $id);
					$pre->execute();
				}else{
					$errorcode = 102;
				}
			}
		}
	}else if($action == 'markdown')
	{
		$errorcode = 2;
		$pre = $dbc->prepare('update userwish set finishedtime=unix_timestamp(now()) where username=? and id=? and finishedtime=0 and status=0');
		$pre->execute(array($username, $id));
		//		$result2 = mysql_query("update userwish set finishedtime='$time' where username='$username' and id='$id' and finishedtime='0' and status='0'");
	}else
	{// no action eq;
		$errorcode = 10;
	}
	
	if(!is_null($pre) && $pre->rowCount() > 0){
		$errorcode = 1;
	}
	
}else{
	$errorcode = 3;
}
echo json_encode(array('errorcode'=>$errorcode));
?>